package com.mmlc.meimei.lichengkongjian.mine;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.customview.SwipeRecyclerView;
import com.mmlc.network.filter.ProfitListRequestFilter;
import com.mmlc.network.responsebean.ProfitResponseBean;
import com.mmlc.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/11/25.
 */
public class ActivityMyProfit extends InterActivity implements View.OnClickListener {

    @InjectView(R.id.iv_back)
    ImageView ivBack;
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.tv_cancelInfo)
    TextView tvCancelInfo;
    @InjectView(R.id.rootView)
    RelativeLayout rootView;
    @InjectView(R.id.tv_totalprofit)
    TextView tvTotalProfit;
    @InjectView(R.id.rc_profit)
    SwipeRecyclerView mRecyclerView;
    @InjectView(R.id.rl_sug)
    RelativeLayout rl_sug;
    private ProfitAdapter adapter;

    @Override
    protected int initContentView() {
        return R.layout.activity_myprofit;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        initUI();
    }
    private boolean isRefresh = false;//是否刷新中
    private boolean isLoadMore = false;
    int page = 0;
    List<ProfitResponseBean.DataBean.StreamBean> stream ;
    private void initUI() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mRecyclerView.setLoadMoreEnable(false);
        tvTitle.setText("我的收益");
        mRecyclerView.getSwipeRefreshLayout().setColorSchemeColors(Color.RED);
        mRecyclerView.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        stream = new ArrayList<>();
        initData(0);
        adapter = new ProfitAdapter(stream);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                isLoadMore = false;
                page = 0;
                initData(page);
            }
            @Override
            public void onLoadMore() {
                page++;
                isLoadMore = true;
                isRefresh = false;
                initData(page);
            }
        });

    }
    boolean  isFirst = true;
    private void initData(int page) {
        ProfitListRequestFilter filter = new ProfitListRequestFilter((BaseParentActivity) activity);
        filter.requestBean.pageNow = page;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<ProfitResponseBean>() {
            @Override
            public void onSuccess(ProfitResponseBean result) {
                if(result!=null){
                    if (!TextUtils.isEmpty(result.getData().getTotal() + "")) {
                        tvTotalProfit.setText(result.getData().getTotal()+"");
                    }
                    if(!TextUtils.isEmpty(result.getData().getTip())){
                        tvCancelInfo.setText(result.getData().getTip());
                    }
                    if(isFirst){
                        stream.clear();
                        stream.addAll(result.getData().getStream());
                        adapter.notifyDataSetChanged();
                        isFirst = false;
                        mRecyclerView.setLoadMoreEnable(true);
                        return;
                    }
                    if(isLoadMore ) {
                        stream.addAll(result.getData().getStream());
                        isLoadMore = false;
                        if(result.getData().getStream().size()==0){
                            mRecyclerView.onNoMore("没有更多数据...");
                        }
                        mRecyclerView.stopLoadingMore();
                        adapter.notifyDataSetChanged();
                        return;
                    }else if(isRefresh){
                        stream.clear();
                        stream.addAll(result.getData().getStream());
                        isRefresh = false;
                        mRecyclerView.complete();
                        adapter.notifyDataSetChanged();
                        return;
                    }else {
                        stream.clear();
                        stream.addAll(result.getData().getStream());
                        adapter.notifyDataSetChanged();
                        return;
                    }

                }else{
                    ToastUtils.showToast("数据类型错误");
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess.toString());
            }
        });
    }



    @OnClick(R.id.tv_cancelInfo)
    public void onClick(View view) {
        rl_sug.setVisibility(View.GONE);
    }

    public class ProfitAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

        List<ProfitResponseBean.DataBean.StreamBean> datas;

        public ProfitAdapter(List<ProfitResponseBean.DataBean.StreamBean> datas) {
            this.datas = datas;
        }

        public  static final int TYPE_MONTH = 1,TYPE_MONTHDETAIL = 2;

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            if(viewType == TYPE_MONTH){
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_month_item, null);
                MonthVh monthVh = new MonthVh(view);
                return monthVh;
            }

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_monthdetail_item, null);
            ProfitDetailVh profitDetailVh = new ProfitDetailVh(view);
            return profitDetailVh;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            if(holder instanceof MonthVh){
                MonthVh vh = (MonthVh) holder;
                int num = 0;
                int flagposition = 0;

                if(position == 0){
                    flagposition = 0;
                }else {
                    for (int i = 0; i < datas.size(); i++) {
                        if (position == (num + i)) {
                            flagposition = i;
                            break;
                        }
                        num += datas.get(i).getList().size();
                    }
                }
                ProfitResponseBean.DataBean.StreamBean streamBean = datas.get(flagposition);
                vh.tv_month.setText(streamBean.getMonth());

                vh.tv_num.setText("+"+streamBean.getMonthtotal());


            }

            if(holder instanceof  ProfitDetailVh){
                ProfitDetailVh vh = (ProfitDetailVh) holder;
                int x = 0;
                int y = 0;
               for (int i = 0; i < datas.size(); i++) {
                    int num = 0;
                    for (int k = 0; k < datas.get(i).getList().size(); k++) {
                        num++;
                        if (position == i + num) {
                            x = i;
                            y = k;
                            break;
                        }
                    }
                   break;
                }

                ProfitResponseBean.DataBean.StreamBean.ListBean listBean = datas.get(x).getList().get(y);
                vh.tv_detailid.setText(listBean.getId());
                vh.tv_detaildate.setText(listBean.getAdd_time());
                vh.tv_detailnum.setText("+"+listBean.getOrder_price());



            }

        }

        @Override
        public int getItemCount() {
            int num = 0;
            for (int i = 0; i < datas.size(); i++) {
                num+=datas.get(i).getList().size();
            }
            int k =datas.size()+num;
            return datas.size()+num;
        }

        @Override
        public int getItemViewType(int position) {
            int previous = 0;
            for (int i = 0; i < datas.size(); i++) {

                previous +=datas.get(i).getList().size();

                if(position == 0){
                    return TYPE_MONTH;
                }
                if(position == previous+i+1){
                    return TYPE_MONTH;
                }
            }

            return TYPE_MONTHDETAIL;
        }

        class MonthVh extends RecyclerView.ViewHolder{

            TextView tv_month,tv_num;

            public MonthVh(View itemView) {
                super(itemView);
                tv_month = (TextView) itemView.findViewById(R.id.tv_monthnum);
                tv_num = (TextView) itemView.findViewById(R.id.tv_monthtotal);

            }
        }

        class ProfitDetailVh extends RecyclerView.ViewHolder{

            TextView tv_detailid,tv_detaildate,tv_detailnum;

            public ProfitDetailVh(View itemView) {
                super(itemView);
                tv_detailid = (TextView) itemView.findViewById(R.id.tv_detailid);
                tv_detaildate = (TextView) itemView.findViewById(R.id.tv_detaildate);
                tv_detailnum = (TextView) itemView.findViewById(R.id.tv_detailnum);
            }
        }

    }
}
